package cn.itsource.pethome.department.service.impl;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.department.domain.Employee;
import cn.itsource.pethome.department.domain.Shop;
import cn.itsource.pethome.department.domain.ShopAuditLog;
import cn.itsource.pethome.department.domain.ShopVo;
import cn.itsource.pethome.department.mapper.EmployeeMapper;
import cn.itsource.pethome.department.mapper.ShopAuditLogMapper;
import cn.itsource.pethome.department.mapper.ShopMapper;
import cn.itsource.pethome.department.service.IShopService;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.util.BaiduAuditUtils;
import cn.itsource.pethome.util.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author majun
 * @since 2022-10-09
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    JavaMailSender mailSender;

    public void registerUser(Shop shop){
//     * 1.验证参数是否正确(判断是否为null)
        if(shop.getName() == null){
            throw new BusinessException("参数不能为空！！！");
        }
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("店铺名违规！！！");
        }
//     * 2.判断当前店铺是否已经被注册
        Integer integer= shopMapper.shopByName(shop.getName());
        if(integer > 0){
            throw new BusinessException("店铺已存在！！！");
        }
//     * 4.保存管理员
        employeeMapper.insert(shop.getAdmin());
        //获取入驻户的id存入店铺表中
        Long id = shop.getAdmin().getId();
        shop.setAdminId(id);
        shopMapper.insert(shop);
        shop.getAdmin().setShop_id(shop.getId());
        employeeMapper.update(shop.getAdmin());
    }

    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //shopAuditLog的参数为店铺id及审核内容
        //1.审核通过查询店铺的状态进行更改
        Shop shop = shopMapper.queryOne(shopAuditLog.getShop_id());
        shop.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        shopMapper.update(shop);
        //2.添加日志信息
        shopAuditLog.setAudit_id(333L);
        shopAuditLog.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        shopAuditLogMapper.insert(shopAuditLog);
        Employee manager = employeeMapper.queryOne(shop.getAdminId());
        System.out.println(manager);
        //给店铺管理员发送邮件激活
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            //使用工具类来构建消息
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            //
            helper.setFrom("1263913263@qq.com");
            //设置邮件主题
            helper.setSubject("mj宠物之家审核通知");
            //邮件正文
            helper.setText("<h1>审核失败</h1><br>"+
                    "<span>失败原因："+shopAuditLog.getNote()+"</span><br>"+
                    "<a href='http://127.0.0.1:8081/#/register2?id="+shop.getId()+"'>点击修改资料</a>",true);
            //发送人
            helper.setTo(manager.getEmail());
            //发送消息
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {

            e.printStackTrace();
        }
    }

    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //shopAuditLog的参数为店铺id及审核内容
        //1.审核通过查询店铺的状态进行更改
        Shop shop = shopMapper.queryOne(shopAuditLog.getShop_id());
        shop.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
        shopMapper.update(shop);
        //2.添加日志信息
        shopAuditLog.setAudit_id(333L);
        shopAuditLog.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
        shopAuditLogMapper.insert(shopAuditLog);
        Employee manager = employeeMapper.queryOne(shop.getAdminId());
        System.out.println(manager);
        //给店铺管理员发送邮件激活
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            //使用工具类来构建消息
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            //
            helper.setFrom("1263913263@qq.com");
            //设置邮件主题
            helper.setSubject("mj宠物之家审核通知");
            //邮件正文
            helper.setText("<h1>审核成功</h1><br>"+
                    "<span>审过结果："+shopAuditLog.getNote()+"</span><br>"+
                    "<a href='http://127.0.0.1:8082/#/register2?id="+shop.getId()+"'>点击激活店铺</a>",true);
            //发送人
            helper.setTo(manager.getEmail());
            //发送消息
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {

            e.printStackTrace();
        }
    }

    @Override
    public List<ShopVo> echarts() {
        return shopMapper.echarts();
    }

    @Override
    public List<ShopVo> echarts1(Date date) {
        return shopMapper.echarts1();
    }

    @Override
    public List<Integer> typecunt() {
        return shopMapper.typecunt();
    }

}
